
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

#include <SFML\OpenGL.hpp>
#include <SFML\Window.hpp>
#include <SFML\Graphics.hpp>
#include "GameState.h"
#include "Screens\ScreenBase.h"

#include <SFML\Network.hpp>

using namespace std;


void loadResources(GameState &gameState){
	TextureManager &textureManager = gameState.getTextureManager();
	textureManager.loadTexture("logo.png");
	textureManager.loadTexture("homeBackground.png");
	textureManager.loadTexture("background.png");
	textureManager.loadTexture("backgroundIsle1.png");
	textureManager.loadTexture("isle1.png");
	textureManager.loadTexture("frozenIsle1.png");
	textureManager.loadTexture("turtoze.png");
	textureManager.loadTexture("platformCenter.png");
	textureManager.loadTexture("platformSide.png");
	textureManager.loadTexture("platformCenterIce.png");
	textureManager.loadTexture("platformSideIce.png");
	textureManager.loadTexture("iceBlock.png");
	textureManager.loadTexture("stoneBlock.png");
	textureManager.loadTexture("cog.png");
	textureManager.loadTexture("greendot.png");
	textureManager.loadTexture("checkpoint.png");
	textureManager.loadTexture("spikes.png");
	textureManager.loadTexture("uiLayout.png");

	ShaderManager &shaderManager = gameState.getShaderManager();
	shaderManager.loadShader("homeFire");
	shaderManager.loadShader("player");
	shaderManager.loadShader("fire");
	shaderManager.loadShader("fireAura");
	shaderManager.loadShader("particle");
	shaderManager.loadShader("iceParticle");
	shaderManager.loadShader("glow");

	SoundManager &soundManager = gameState.getSoundManager();
	soundManager.loadSound("fire1.wav");
	soundManager.loadSound("enemyFire.wav");
	soundManager.loadSound("dot.wav");
	soundManager.loadSound("cog.wav");
	soundManager.loadSound("checkpoint.wav");
	soundManager.loadSound("jump.wav");
}



int main()
{
	sf::RenderWindow window(sf::VideoMode(800, 600), "Tristesa", Style::Close);

	RenderTexture renderTexture;
	renderTexture.create(800, 600);
	renderTexture.setView(window.getDefaultView());

	renderTexture.setActive();
	GameState gameState(renderTexture);
	

	loadResources(gameState);
	gameState.getCurrentScreen()->init();

	sf::Clock clock;
	window.setVerticalSyncEnabled(true);


    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed){
                window.close();	
			}
        }

		sf::Time time = clock.restart();
		gameState.update(time.asSeconds());

		renderTexture.setActive();
		gameState.draw();
		renderTexture.display();
		

		window.setActive();
		
		Sprite renderSprite(renderTexture.getTexture());
		
		Shader *glow = gameState.getShaderManager().getShader("glow");
		glow->setParameter("screenTexture", *renderSprite.getTexture());
		window.draw(renderSprite, glow);
		window.display();

		

    }

    return 0;
}